Repository navigation
Conversation
An HTML entry point turned all of its <script src> tags into import records on one statement-less part. The linker then inlined every script into one flat module, so: - a script with a top-level await held back every script after it (a later classic document.write script ran after the page loaded and erased it, only in production builds) - a classic script that the bundler wraps in __commonJS never ran - a script that is also import()ed ran when the import() fired, not where its tag was Each <script src> now becomes a bare import statement in a part of its own, in document order. The linker calls a wrapped script's wrapper where the tag was, and a script with a top-level await that has a later sibling gets an async __esm wrapper that the HTML module starts in place and awaits at the end of the chunk, like the browser runs separate module scripts and like the dev server already did.
|
Status Reproduced with The branch fixes all three by giving the HTML module one |
|
#42026 carries only the base fix that this draft also contains: one part per |
The chunk prints wrapped files ahead of flat code, so this is the case where the wrapper call must come from the tag's own part to keep document order. Adapted from the html/commonjs-script-runs-in-place case in #41999.
|
Closing in favor of #42026. It gives each HTML The top-level-await ordering rule from this branch was not carried over. It changes when later scripts run, which needs a maintainer decision, and #41982 already covers that question ( |
Problem
bun build ./index.htmlandBun.serve({ development: false })flatten a page's<script src>tags into one module, so a script with a top-levelawaitholds back every later script. A classicdocument.writesnippet then runs after load and erases the page. The dev server is fine.__commonJSwrapper that nothing calls, so it never runs. A script that is alsoimport()ed runs at theimport(), not at its tag.ParseTask.rs,Loader::Html) gave the page module import records but no import statements, so the linker had no import sites to order or call wrappers from.Fix
<script src>becomes a bareimportin its own part, in document order. A wrapped script's wrapper is now called where its tag was.__esmwrapper. The page module starts it in place, unawaited, and awaits it at the end of the chunk, as browsers and the dev server run separate module scripts.test/bundler/bundler_html.test.ts(5 new cases, 4 fail on 1.4.3). Also the HTML serve, manifest, splitting and dev server suites.Background
import()target without splitting) gets a wrapper,require_xorinit_x, that each import site calls, withawaitwhen it is async.<script>element.Notes
await init_x()in place, so a top-level await still holds back the later scripts (the reported case). If it lands first, this PR rebases onto it and keeps only the top-level-await rule, the tail await and their tests.__script(() => {})error boundary and also covers the top-level await case. It emits the wrapper calls next to the wrapper definitions, which the chunk prints ahead of all flat code, so with tags[first, umd, tla, last]that branch runsumd, tla, first, last. This PR keeps document order by emitting the calls from the HTML module's own parts, adds no runtime helper, and an error-boundary layer could sit on top of it.import()-without-splitting machinery), so only pages that combine a top-level await with a later script pay for closures. The last script of a page never needs one.await init_x()keeps two properties of today's output: the chunk's evaluation finishes after every script has, and a rejected top-level await surfaces as the module script's error.document.writefrom a module script is ignored while the script runs synchronously, but it reopens (erases) the document once the parser is done, which is what the delayed classic script hit.require_x();with no namespace object, so__toESMis not pulled in.find_imported_css_files_in_js_orderstill walks them in document order.<script src="https://...">tags stay in the HTML as before; their import statements are dropped from the chunk.--splitting, a top-level-await script shared by two pages lands in a shared chunk that exportsinit_x; each page chunk starts and awaits it. Checked by hand along with--compile --target=browser,--format=iife, a server build that imports the page, and the esbuild and cjs2esm suites.__promiseAllwithout bundling it;init_x()calls are hoisted above earlierrequire_y()imports in the same file.